<?php

/**
 * WPUF subscription manager
 *
 * @since 0.2
 * @author Tareq Hasan
 * @package WP User Frontend
 */
class WPUF_container {
	
	protected $default_menu;

    function __construct() {
    	add_shortcode( 'wpuf_container', array($this, 'shortcode') );
    	add_action( 'init', array($this, 'init_menu') , 100);
    }
    
    
    function init_menu () {
    	$this->default_menu = array (
    			array (
    					'name' => __('My posts', 'wpuf'),
    					'href' => 'dashboard',
    					'current' => false,
    					'content' => '[wpuf_dashboard]'
    			),
    			array (
    					'name' => __('Write post', 'wpuf'),
    					'href' => 'addpost',
    					'current' => false,
    					'content' => '[wpuf_addpost]'
    			),
    			array (
    					'name' => __('Modify post', 'wpuf'),
    					'href' => 'editpost',
    					'current' => false,
    					'content' => '[wpuf_edit]'
    			),
    			array (
    					'name' => __('My profile', 'wpuf'),
    					'href' => 'editprofile',
    					'current' => false,
    					'content' => '[wpuf_editprofile]'
    			)
    	);
    }
    /**
     * Handles the add post shortcode
     *
     * @param $atts
     */
    function shortcode( $atts ) {
    
    	extract( shortcode_atts( array('post_type' => 'post'), $atts ) );
    
    	ob_start();
    
    	if ( is_user_logged_in() ) {
    		$this->post_form( $post_type );
    	} else {
    		printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( get_permalink(), false ) );
    	}
    
    	$content = ob_get_contents();
    	ob_end_clean();
    
    	return $content;
    }
    
    /**
     * Add posting main form
     *
     * @param $post_type
     */
    function post_form( $post_type ) {
    	?>
<div id="wpuf-author-center">
	<table class="wpuf-autor-table">
		<tr>
			<td id="wpuf-author-control">
				<div id="wpuf-author-info">
					<div id="wpuf-author-avatar">
    			<?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentyeleven_author_bio_avatar_size', 60 ) ); ?>
    		</div>
					<!-- #author-avatar -->
					<div id="wpuf-author-description">
				<?php 
				$user_data = wp_get_current_user();
				printf( ' %s ', $user_data->data->user_login ); ?>
    		</div>
					<!-- #author-description	-->
				</div> <!-- #author-info -->

				<ul id="wpuf-author-menu">
    		<?php
    		
    		$author_menu = $this->wpuf_get_author_menu ();
    		foreach ( $author_menu as $key => $item ) {
    			if ($item ['current'] == true) {
    				?>
    				<li class="wpuf-current-menu-item wpuf-menu-li">
    				<?php
    			} else {
    				?>				
					<li class="wpuf-menu-li">
    				<?php
    			}
    			$href = add_query_arg ( 'href', $item ['href'], self::wpuf_get_container_page_url() );
    			?>
    			<a href="<?php echo $href;?>"><b
							class="ico ico-nav <?php echo $item ['href'];?>"></b><?php echo $item['name'];?></a>
					</li>
    			<?php
    		}
    		?>
    			</ul>
			</td>
			<td id="wpuf-author-content">
				<div id="wpuf-content">
    		<?php
    		foreach ($author_menu as $key=>$item) { 
    		if ($item['current'] == true) { 
    			echo do_shortcode($item['content']); break; 
    			} 
    		}
    		?>
    		</div>
			</td>
		</tr>
	</table>
</div>
<!-- #primary -->
<?php
    }
    
    function wpuf_get_wpuf_page_url ($option) {	
		$page_href = array();
		foreach ( $this->default_menu as $key => $item ) {
			$page_href[] = $item ['href'];
		}
    	if (in_array($option, $page_href )) {
    		return add_query_arg('href', $option, self::wpuf_get_container_page_url());
    	}
    	return "";
    }

    function wpuf_get_author_menu() {
    	if (isset ( $_REQUEST ['href'] )) {
    		foreach ( $this->default_menu as $key => $item ) {
    			if ($item ['href'] == $_REQUEST ['href']) {
    				$this->default_menu[$key]['current'] = true;
    				break;
    			}
    		}
    	} else {
    		foreach ( $this->default_menu as $key => $item ) {
    			$this->default_menu[$key]['current'] = true;
    			break;
    		}
    	}
    	return $this->default_menu;
    }
    

   public static function wpuf_create_container_page() {
    	$page_id = self::wpuf_get_container_page_id();
    	if (!isset($page_id)) {
    		$page_id = wp_insert_post( array(
    				'post_title'     => __('User centre', 'wpuf'),
    				'post_name'      => 'profile_container_page',
    				'post_status'    => 'publish',
    				'post_type'      => 'page',
    				'post_content'   => '[wpuf_container]',
    				'comment_status' => 'closed',
    				'ping_status'    => 'closed'
    		) );
    		update_post_meta( $page_id, '_wpuf_action', 'profile_container_page' );
    	}
    }
    
   public static function wpuf_get_container_page_id () {
    	global $wpdb;
    	if ( ! $page_id = wp_cache_get( 'wpuf_container_page', 'wpuf_page_ids' ) ) {
    		$page_id = $wpdb->get_var( $wpdb->prepare( "SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->postmeta pmeta ON p.ID = pmeta.post_id WHERE p.post_type = 'page' AND pmeta.meta_key = '_wpuf_action' AND pmeta.meta_value = %s", 'profile_container_page') );
    		if ( ! $page_id )
    			return null;
    		wp_cache_add( 'wpuf_container_page', $page_id, 'wpuf_page_ids' );
    	}
    	return $page_id;
    }
    
   public static function wpuf_get_container_page_url() {
		if (!$page_url = wp_cache_get('wpuf_container_url', 'wpuf_page_ids')) {
    	$page_id = self::wpuf_get_container_page_id ();
			if (isset ( $page_id )) {
				$page_url = get_permalink ( $page_id );
				wp_cache_add('wpuf_container_url', $page_url, 'wpuf_page_ids');
			}
		}
		return $page_url;
	}
	public static function wpuf_is_current_container_page() {
		$current_url = home_url ( add_query_arg ( array() ) );
		$container_page_url = self::wpuf_get_container_page_url ();
		if (isset ( $container_page_url )) {
			if (false !== strpos ( $current_url, $container_page_url, 0 )) {
				return true;
			}
		}
		return false;
	}
	
	public static function wpuf_delete_container_page () {
    	$pages = get_posts (
    			array(
    					'post_type'      => 'page',
    					'post_status'    => 'any',
    					'meta_key'       => '_wpuf_action',
    					'posts_per_page' => -1
    			)
    	);
    	// Delete pages
    	foreach ( $pages as $page ) {
    		wp_delete_post( $page->ID, true );
    	}
    	
    	wp_cache_delete('wpuf_container_page', 'wpuf_page_ids');
    	wp_cache_delete('wpuf_container_url', 'wpuf_page_ids');
    }

}

$wpuf_container = new WPUF_container();